package com.wu.util;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @ClassName: CombineUtils
 * @Author: xiaox
 * @Description:
 * @Date: 2022/11/17 15:53
 * @Version: 1.0
 */
public class CombineUtils {

    /**
     * @param list1
     * @param list2
     * @param list3
     * @return
     */
    public static List<String> permutationNoRepeat(List<String> list1, List<String> list2, List<String> list3) {

        return list1.stream().flatMap(str -> list2.stream().map(s -> ":" + s).map(str::concat)).flatMap(str -> list3.stream().map(s -> ":" + s).map(str::concat)).collect(Collectors.toList());
    }

    public static List<String> permutationNoRepeatString(List<String> list1, List<String> list2) {

        return list1.stream().flatMap(str -> list2.stream().map(s -> "_" + s).map(str::concat)).collect(Collectors.toList());
    }


    public static List<Double> permutationNoRepeatWeight(List<Double> list1, List<Double> list2, List<Double> list3) {

        return list1.stream().flatMap(dou -> list2.stream().map(i -> 10*dou*i)).flatMap(dou -> list3.stream().map(i -> 10*dou*i)).map(d->{
            DecimalFormat df = new DecimalFormat("#####0.00");
            String s = df.format(d);
            return Double.valueOf(s);
        }).collect(Collectors.toList());
    }

    public static List<Double> permutationNoRepeatDouble(List<Double> list1, List<Double> list2) {

        return list1.stream().flatMap(dou -> list2.stream().map(i -> 100*dou*i)).map(d->{
            DecimalFormat df = new DecimalFormat("#####0.00");
            String s = df.format(d);
            return Double.valueOf(s);
        }).collect(Collectors.toList());
    }


    /**
     * 求交集
     * @param nums1
     * @param nums2
     * @return
     */
    public static List<Long> intersection(List<Long> nums1, List<Long> nums2) {
        if (nums1 == null || nums1.isEmpty() || nums2 == null || nums2.isEmpty()) {
            return new ArrayList<>() ;
        }
        Set<Long> set1 = new HashSet<>();
        Set<Long> resSet = new HashSet<>();
        //遍历数组1
        set1.addAll(nums1);
        //遍历数组2的过程中判断哈希表中是否存在该元素
        for (long i : nums2) {
            if (set1.contains(i)) {
                resSet.add(i);
            }
        }
        List<Long> resArr = new ArrayList<>(resSet.size());
        //将结果几何转为数组
        resArr.addAll(resSet);
        return resArr;
    }

}
